<?php
namespace Apps\Csoap\Controllers;

use Phalcon\Mvc\Controller;
use Apps\Csoap\Controllers\BaseController;
use Phalcon\Http\Response;
use PhalconPlus\Common\ResponseResultStatus;
use Apps\Common\Models\Branch;
use Phalcon\Mvc\Model;

class BranchController extends BaseController
{
    public function initialize(){
        parent::initialize();
    }
    /**
     * 申请开通线上店铺
     * @date: 2016年2月27日 
     * @author: 545
     */
    public function applyAction(){
        
        $this->view->disable();
        
        $shop_name = $this->getDataItem('shop_name');//分店名称
        $shop_id = $this->getDataItem('shop_id');//分店id
        $shop_uid = $this->getDataItem('shop_uid');//分店店长id
        $agent_name = $this->getDataItem('agent_name');//代理商名称
        $agent_id = $this->getDataItem('agent_id');//代理商id
        $start_time = $this->getDataItem('start_time');//授权开始时间
        $end_time = $this->getDataItem('end_time');//授权结束时间
        $longtime = $this->getDataItem('longtime');//授权时长
       
        if (empty($shop_id)||empty($shop_name)||empty($agent_name)||empty($start_time)||empty($end_time)||empty($longtime)){
            $this->senderror(ResponseResultStatus::POST_BODY_PARAM_ERROR,'业务数据不全');
        }
        
        //添加到记录表
        $data['branch_name'] = $shop_name;
        $data['shop_id'] = $shop_id;
        $data['shop_uid'] = $shop_uid;
        $data['long_time'] = $longtime;
        $data['start_time'] = $start_time;
        $data['end_time'] = $end_time;
        $data['status'] = 100;
        $data["agent_name"] = $agent_name;
        $data['agent_id'] = $agent_id;
        $data['create_time'] = time();
        
        $info = Branch::findFirst("shop_id = {$shop_id} AND agent_id = {$agent_id}");
        
        if ($info){
            $this->senderror(ResponseResultStatus::REPEAT_REQUEST,'已存在');
        }
        $branch = new Branch();
        if($branch->save($data)){
            $this->sendResult(ResponseResultStatus::SUCCESS,['id'=>$branch->branch_id,'success'=>1],'');
        }
        else{
            $this->senderror(ResponseResultStatus::DATABASE_ERROR,'写入失败');
        }
    }
}